﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Axis.Dao.Research;
using PhuLeBooze.Global;
using System.Xml;
using Axis.BusinessObject.Research;
using Axis.BusinessObject.Core;

namespace PhuLeBooze.Views.Project
{
    public class CQuestionPathInput
    {
        public int QuestionID;
        public int QuestionType;
        public string ResponseValue;
        public string ResponseText;
    }

    public class CQuestionPathUI
    {
        public string GetXmlRequest(HttpContext context)
        {
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("Root");
            XmlElement InputValue = doc.CreateElement("InputValue");
            HttpRequest request = context.Request;
            string Stop = request["Stop"].Trim();
            InputValue.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());

            InputValue.SetAttribute("ProjectID", request["ProjectID"].Trim());
            if (request["Description"] != null)
                InputValue.SetAttribute("Description", request["Description"].Trim());
            if (request["ID"] != null)
                InputValue.SetAttribute("ID", request["ID"].Trim());
            InputValue.SetAttribute("Status", request["Status"].Trim());
            InputValue.SetAttribute("Action", request["Action"].Trim());
            if (request["Name"] != null)
                InputValue.SetAttribute("Name", request["Name"].Trim());
            if (request["ZOrder"] != null)
                InputValue.SetAttribute("ZOrder", request["ZOrder"].Trim());
            root.AppendChild(InputValue);
            string temp = HttpUtility.HtmlDecode(request["ResponseOption"].Trim());
            IList<CQuestionPathInput> List = GetPathList(temp);
            for (int i = 0; i < List.Count; i++)
            {
                var row = List[i];
                XmlElement ele = doc.CreateElement("Detail");
                ele.SetAttribute("QuestionnaireID", row.QuestionID.ToString());
                if (row.QuestionType != 3)
                    ele.SetAttribute("ResponseValue", row.ResponseValue);
                if (i == List.Count - 1)
                {
                    if (Stop == "0" || Stop == "-100")
                        ele.SetAttribute("NextQuestionnaireID", Stop);
                }
                else
                    ele.SetAttribute("NextQuestionnaireID", List[i + 1].QuestionID.ToString());

                root.AppendChild(ele);

            }
            return root.InnerXml;
        }

        public IList<CQuestionPathInput> GetPathList(string InputText)
        {

            JObject googleSearch = JObject.Parse(InputText);
            // get JSON result objects into a list
            IList<JToken> results = googleSearch["ResponseOption"].Children().ToList();
            // serialize JSON results into .NET objects
            IList<CQuestionPathInput> searchResults = new List<CQuestionPathInput>();
            foreach (JToken result in results)
            {
                CQuestionPathInput searchResult = JsonConvert.DeserializeObject<CQuestionPathInput>(result.ToString());
                searchResults.Add(searchResult);
            }
            return searchResults;
        }

        public IList<CQuestionPathDetail> GetQuestionPathDetailList(int PathID, int ProjectID)
        {
            CResearchDao core = new CResearchDao();
            string InputValue = string.Format("<InputValue UserID=\"{0}\" Session=\"{1}\" QuestionPathID=\"{2}\" ProjectID=\"{3}\"/>", CSecurity.USER_ID.ToString(), CSecurity.SECRECT_TOKEN.ToString(), PathID, ProjectID);
            return core.GetQuestionPathDetail(InputValue);
        }


        public IList<CQuestionnaireConditionDetail> GetQuestionnaireConditionsDetails( int ProjectID,int PathID)
        {
            CResearchDao core = new CResearchDao();
            string InputValue = string.Format("<InputValue UserID=\"{0}\" Session=\"{1}\" QuestionnaireConditionsID=\"{2}\" ProjectID=\"{3}\"/>", CSecurity.USER_ID.ToString(), CSecurity.SECRECT_TOKEN.ToString(), PathID, ProjectID);
            return core.GetQuestionnaireConditionsDetails(InputValue);
        }

        public IList<CQuestionnaireCondition> GetQuestionnaireConditions(int ProjectID, int PathID)
        {
            CResearchDao core = new CResearchDao();
            string InputValue = string.Format("<InputValue UserID=\"{0}\" Session=\"{1}\" ID=\"{2}\" ProjectID=\"{3}\"/>", CSecurity.USER_ID.ToString(), CSecurity.SECRECT_TOKEN.ToString(), PathID, ProjectID);
            return core.GetQuestionnaireConditions(InputValue);
        }

        public CApplicationMessage ActionQuestionnaireConditions(string InputValue)
        {
            CResearchDao core = new CResearchDao();
            InputValue=InputValue.Replace("[KeyExtend]", string.Format(" UserID=\"{0}\" Session=\"{1}\" ", CSecurity.USER_ID.ToString(), CSecurity.SECRECT_TOKEN.ToString()));
            return core.ActionQuestionnaireConditions(InputValue);
        }
    }
}